package com.shao.web;

import com.shao.pojo.User;
import com.shao.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * User: SJP
 * Date: 2021/5/16
 * Time: 17:24
 * Description: No Description
 */
public abstract class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    //当有请求来的时候，由提交的表单的method决定调用
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决post请求中文乱码问题
        // 一定要在获取请求参数之前调用才有效
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=utf-8");
        String action = req.getParameter("action");
        System.out.println(action);
        System.out.println("执行了post");
//        if(action.equals("login")){
//            login(req,resp);
//        }else if(action.equals("regist")){
//            regist(req,resp);
//        }
        //通过反射，将action对应方法名通过方射获取，再通过方法的invoke调用
        try {
            Method Method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            Method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
